package com.atcumt.Thread.ProductorAndConsumer;

import java.util.LinkedList;

public class waitAndNotifyDemo {

    private int i  = 1;
    private int maxSize;
    private LinkedList<Object> storage;

    public waitAndNotifyDemo(int size) {
        this.maxSize = size;
        storage = new LinkedList<>();
    }

    public synchronized void put() throws InterruptedException {
        while (storage.size() == maxSize) {
            wait();
        }
        storage.add(new Object());
        System.out.println("生产者生产：" + (i++));
        notifyAll();
    }

    public synchronized void take() throws InterruptedException {
        while (storage.size() == 0) {
            wait();
        }
        storage.remove();
        int size = storage.size();
        System.out.println("消费者消费后剩余：" + size--);
        notifyAll();
    }
}
